import { Component, OnInit, ViewChild } from "@angular/core";
import { NzMessageService } from "ng-zorro-antd/message";
import { ActivatedRoute, Router } from "@angular/router";
import { NzTableQueryParams } from "ng-zorro-antd/table";
import { DeviceMaintainTaskService } from "./device-maintain-task.service";
import { DeviceMaintainTaskEditComponent } from "./device-maintain-task-edit/device-maintain-task-edit.component";

@Component({
  selector: 'app-device-maintain-task',
  templateUrl: './device-maintain-task.component.html',
  styles: [
  ]
})
export class DeviceMaintainTaskComponent implements OnInit {


  loading = false;

  // 设备组数据
  deviceGroups = [];

  // 分页参数
  pageData = { page: 0, size: 10, total: null };
  // 查询条件
  searchBeans = [{
    attributeName: "code",
    searchType: "LIKE",
    attributeValue: ""
  }, {
    attributeName: "taskStatus",
    searchType: "EQ",
    numberValue: null
  }];

  @ViewChild("deviceMaintainTaskEditComponent", { static: false })
  public deviceMaintainTaskEditComponent: DeviceMaintainTaskEditComponent;

  constructor(private deviceMaintainTaskService: DeviceMaintainTaskService,
              private msgSrv: NzMessageService,
              ) {
  }

  ngOnInit(): void {

    this.loadListData();
  }

  _onReuseInit() {
    this.loadListData();
  }

  delete(id?) {
    this.loading = true;
    this.deviceMaintainTaskService.delete(id).subscribe((res) => {
      this.loading = false;
      if (res.code === 1) {
        this.msgSrv.success("删除完成");
      } else {
        this.msgSrv.error(res.message);
      }
    });
    console.log(id);


  }

  onSmt() {
    this.loadListData();
  }

  cancel() {
    this.msgSrv.success("已取消");
  }

  openEdit(record): void {

    if(record.taskStatus === 1){
      this.msgSrv.warning("此保养任务已完成");
      return;
    }

    this.deviceMaintainTaskEditComponent.edit(record)

  }

  openView(record) {
    this.deviceMaintainTaskEditComponent.view(record);
  }


  /**
   * 清除查询参数
   */
  clearParam() {

    this.searchBeans.forEach((item) => {
      item.attributeValue = "";
    });
  }

  /**
   * 加载列表数据
   */
  loadListData() {

    this.loading = true;

    const searchData = {
      page: this.pageData.page,
      size: this.pageData.size,
      searchFilter: {
        searchBeans: this.searchBeans
      }
    };

    this.deviceMaintainTaskService.findPage(searchData).subscribe((res) => {

      this.loading = false;

      if (res.code === 1) {
        this.pageData.page = res.data.number;
        this.pageData.size = res.data.size;
        this.pageData.total = res.data.totalElements;
        this.deviceGroups = res.data.content;
      } else {
        this.msgSrv.error(res.message);
      }

    });
  }

  onQueryParamsChange(params: NzTableQueryParams): void {

    const { pageSize, pageIndex, sort, filter } = params;

    this.pageData.page = pageIndex - 1;
    this.pageData.size = pageSize;

    this.loadListData();
  }

}
